﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Core.Localization;

namespace MvcEngine.Core.Validation
{
    /// <summary>
    /// Base class for all property validators. Each implementation of a <see cref="FieldValidatorAttribute"/> has the Message property that contains the message that should be displayed if the validation of that property is failed.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
    public abstract class FieldValidatorAttribute : ValidatorBase
    {
        protected string Message;

        public FieldValidatorAttribute(string message)
            : this(message, true)
        {
        }

        public FieldValidatorAttribute(string message, bool isLocalizable)
        {
            if (isLocalizable)
            {
                string res = ResourceReader.Current.GetString(message);
                if (string.IsNullOrEmpty(res))
                {
                    Message = message;
                }
                else
                {
                    Message = res;
                }
            }
            else
            {
                Message = message;
            }

            this.IsLocalizable = isLocalizable;
        }
    }
}
